﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Laos;
using System.Text.RegularExpressions;
using Pollux.Interpreter.Venus2.Exceptions;

namespace Pollux.Interpreter.Venus2.Aspects
{
	[Serializable]
	[AttributeUsage(AttributeTargets.Property)]
	public class RegexValidator : OnMethodBoundaryAspect
	{
		//public namend fields;
		public bool Matching = true;

		private Regex expression;
		public RegexValidator (string regExprPattern)
		{
			expression = new Regex( regExprPattern, RegexOptions.Compiled);
		}

		public override void  OnEntry(MethodExecutionEventArgs eventArgs)
		{
 			base.OnEntry(eventArgs);

			Object[] args = eventArgs.GetArguments();
			if (args == null)
			{
				//getter
				return;
			}

			String input = (String)args[0];

			if( expression.IsMatch( input ) == Matching )
			{				
				throw new ArgumentException ( (Matching ? "Keine":"") + "Übereinstimmung mit Pattern");
			}
		}
	}
}
